#!/usr/bin/env Python
# coding=utf-8
from sqlalchemy import Column
from sqlalchemy.types import CHAR, String, DateTime, UnicodeText, BigInteger, Boolean, Integer, Text, Float
from sqlalchemy import func, or_, not_

import methods.database.dbORM as dbORM

def init_db():
    dbORM.BaseModel.metadata.create_all(dbORM.engine)

def drop_db():
    dbORM.BaseModel.metadata.drop_all(dbORM.engine)

def getRoomPlayer(data):
    param=""
    for i in data:
        param=param+","+i+"='"+data[i]+"'"
    query=dbORM.session.query(RoomPlayer).filter(param[1:]).scalar()
    return query

def enterRoom(data):
    roomPlayer = RoomPlayer()
    for i in data:
        setattr(roomPlayer,i,data[i])
    dbORM.session.add(roomPlayer)
    dbORM.session.commit()

def quitRoom(data):
    param="username='"+data+"'"
    x = dbORM.session.query(RoomPlayer).filter(param).one()
    dbORM.session.delete(x)
    dbORM.session.commit()


class RoomPlayer(dbORM.BaseModel):
    __tablename__ = 'room_player'
    id = Column(Integer, primary_key=True)
    username = Column(CHAR(255))
    user_ip = Column(CHAR(30))
    room_id = Column(Integer)
    enter_time = Column(DateTime)

    def __repr__(self):
        return "<Room_player(username='%s',  room_id='%s',  user_ip='%s',  enter_time='%s')>" % (self.username, self.room_id, self.user_ip, self.enter_time)
